import axios from "axios";
import { Notification, MessageBox, Message } from "element-ui";
import store from "@/store";
import { getToken } from "@/utils/auth";
import qs from "qs";
import { myvue } from "@/main.js";
import Vue from "vue";

var localurl = window.location.href.split("jypw")[0] + "jypw/";

var serverurl;
// if(localurl.indexOf("localhost") == -1) {
// 	serverurl = localurl;
// } else {
serverurl = process.env.VUE_APP_BASE_API;
// }

export function request(req) {
  let param = {};
  param["url"] = req.url;
  param["method"] = req.method;
  if (req.params) {
    param["params"] = req.params;
  }
  if (req.data) {
    param["data"] = req.data;
  }
  // baseURL: 'http://47.98.108.183:10136/jypw', // 正式服务器地址
  // baseURL: 'http://192.168.3.30:9088/jypw', // CYP服务器地址
  // baseURL: 'http://192.168.3.6:9088/jypw', // 公司本地服务器地址
  // baseURL: 'http://192.168.3.140:9088/jypw', // 测试地址
  // baseURL: 'http://localhost:9088/jypw'
  // baseURL: 'http://192.168.3.177:9088/jypw'
  // baseURL: 'http://192.168.3.177:9088/jypw',

  // let url = 'http://47.98.108.183:10134/jypw';
  // let url = 'http://47.122.7.81:9099/jypw';
  // let url = 'http://localhost:9099/jypw'

  let url = `${process.env.VUE_APP_BASE_API}`;
  //let url = 'jypw'
  sessionStorage.setItem("baseURL", url);
  let Axios = axios.create({
    baseURL: url,
  });
  if (req.showLoading) {
    Vue.$loading.show();
    //		myvue._vnode.child.loading = true;//myvue._vnode.child   这个是app.vue  myvue 是main.js
  }

  let posttype = "";
  if (req.type) {
    if (req.type == 1) {
      //			axios.defaults.headers['Content-Type'] = 'multipart/form-data;charset=utf-8';
      posttype = "multipart/form-data;charset=utf-8";
    }
    if (req.type == 2) {
      posttype = "application/json;charset=utf-8";
      //			axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8';
    }
    console.log(req.type);
  } else {
    posttype = "application/x-www-form-urlencoded";
    if (req.method == "post") {
      if (param.data) {
        param.data = qs.stringify(req.data, { allowDots: true });
      } else {
        param["data"] = qs.stringify(req.params, { allowDots: true });
      }
      if (param.params) {
        delete param.params;
      }
    }
  }

  Axios.interceptors.request.use(
    config => {
      config.headers["token"] = getToken();
      config.headers["Content-Type"] = posttype;
      return config;
    },
    error => {
      myvue.message(error, 3);
    }
  );

  // 响应拦截器
  Axios.interceptors.response.use(
    res => {
      Vue.$loading.hide();
      //		myvue._vnode.child.loading = false;
      const code = res.data.code;
      if (code === "401") {
        MessageBox.confirm(
          "登录状态已过期，您可以继续留在该页面，或者重新登录",
          "系统提示",
          {
            confirmButtonText: "重新登录",
            cancelButtonText: "取消",
            type: "warning",
          }
        ).then(() => {
          store.dispatch("LogOut").then(() => {
            location.reload(); // 为了重新实例化vue-router对象 避免bug
          });
        });
      } else if (code === "190") {
        store.dispatch("LogOut").then(() => {
          location.reload(); // 为了重新实例化vue-router对象 避免bug
        });
      } else {
        if (code === "100") {
          //   let msgtype = res.data.success ? 1 : 3;
          myvue.message(res.data.msg, 3);
        }
        // if (!res.data.success) {
        // 	myvue.message(res.data.msg, 3)
        // }
        return res.data;
      }
    },
    error => {
      console.log("err" + error);
      myvue.message(error.message, 3);
      return Promise.reject(error);
    }
  );
  store.dispatch("refreshNoRequest", 0);
  return Axios(param);
}
